$(function () {
    // 修改三个select的无数据时反馈字符串
    $("#postFirstAddresses").select2({
        language: {
            noResults: function (params) {
                return "无数据";
            }
        }
    });
    $("#postSecondAddresses").select2({
        language: {
            noResults: function (params) {
                return "无数据";
            }
        }
    });
    $("#postThirdAddresses").select2({
        language: {
            noResults: function (params) {
                return "无数据";
            }
        }
    });

    // 地址分类数据
    $.ajax({
        type: 'GET',
        url: '/firstAddressCategories',
        success: function (result) {
            let postFirstAddresses = $('#postFirstAddresses');
            let postSecondAddresses = $('#postSecondAddresses');
            let postThirdAddresses = $('#postThirdAddresses');
            // 先清空之前的分类
            postFirstAddresses.empty();
            postSecondAddresses.empty();
            postThirdAddresses.empty();
            if (result.statusCode === 200) {
                selectCreateForAddress(postFirstAddresses, result.data); // 一级分类
                selectCreateForAddress(postSecondAddresses, result.data[0].secondAddressCategories); // 二级分类
                selectCreateForAddress(postThirdAddresses, result.data[0].secondAddressCategories[0].thirdAddressCategories); // 三级分类
            } else {
                selectCreateForAddressError(postFirstAddresses, result.message); // 一级分类显示错误
            }
        }
    });

    // 二、三级地址变化
    $('#postFirstAddresses').on('change', function () {
        let addressId = $(this).find('option:selected').val();
        $.ajax({
            type: 'GET',
            url: '/secondAddressCategories',
            data: {addressId: addressId},
            success: function (result) {
                let postSecondAddresses = $('#postSecondAddresses');
                let postThirdAddresses = $('#postThirdAddresses');
                // 先清空之前的
                postSecondAddresses.empty();
                postThirdAddresses.empty();
                if (result.statusCode === 200) {
                    selectCreateForAddress(postSecondAddresses, result.data);
                    selectCreateForAddress(postThirdAddresses, result.data[0].thirdAddressCategories);
                } else {
                    selectCreateForAddressError(postSecondAddresses, result.message);
                }
            }
        });
    });

    // 三级地址变化
    $('#postSecondAddresses').on('change', function () {
        let addressId = $(this).find('option:selected').val();
        $.ajax({
            type: 'GET',
            url: '/thirdAddressCategories',
            data: {addressId: addressId},
            success: function (result) {
                let postThirdAddresses = $('#postThirdAddresses');
                // 先清空之前的
                postThirdAddresses.empty();
                if (result.statusCode === 200) {
                    selectCreateForAddress(postThirdAddresses, result.data);
                } else {
                    selectCreateForAddressError(postThirdAddresses, result.message);
                }
            }
        });
    });
});

function selectCreateForAddress(addresses, data) {
    for (let i = 0; i < data.length; i++) {
        let option = $('<option>');
        if (i === 0) {
            option.attr('selected', 'selected');
        }
        option.attr('value', data[i].addressId);
        option.text(data[i].addressName);
        addresses.append(option);
    }
}

function selectCreateForAddressError(addresses, message) {
    let option = $('<option>');
    option.attr('selected', 'selected');
    option.text(message);
    addresses.append(option);
}